package cn.tecnova.cd

import cn.tecnova.utils.ConfigHandler
import org.apache.log4j.{Level, Logger}
import org.apache.spark.sql.{Dataset, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.elasticsearch.spark.rdd.EsSpark

/**
  * description:
  * 14 2057
  **/
object TecnovaCopyright {
  Logger.getLogger("org").setLevel(Level.ERROR)

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
      .setAppName(this.getClass.getSimpleName)
      //      .setMaster("local[*]")
      .set("es.index.auto.create", "true")
      .set("es.nodes", ConfigHandler.esNodes)
      .set("es.port", ConfigHandler.esPort)
      .set("es.nodes.wan.only", "true")

    val sc = new SparkContext(conf)
    val sqLContext = new SQLContext(sc)

    import sqLContext.implicits._
    val rowDF = sqLContext.read.jdbc("jdbc:mysql://www.slave4.com:3306/tec_copyright", "qichacha_copyright", ConfigHandler.props2)


    //导入写es相关的包
    import org.elasticsearch.spark.sql._

    val result: Dataset[TecnovaCopyright] = rowDF.map(row => {
      TecnovaCopyright(
        row.getAs[Int]("id"),
        row.getAs[String]("KeyNo"),
        row.getAs[String]("cr_id"),
        row.getAs[String]("Category"),
        row.getAs[String]("Name"),
        row.getAs[String]("Owner"),
        row.getAs[String]("RegisterNo"),
        row.getAs[String]("RegisterDate"),
        row.getAs[String]("FinishDate"),
        row.getAs[String]("PublishDate")

      )
    })
//    result.saveToEs("tecnova_copyright2" + "/company_copyright")
    EsSpark.saveToEs(result.rdd,"tecnova_copyright/company_copyright",Map("es.mapping.id" -> "id"))


    sc.stop()

  }

}


case class TecnovaCopyright(

                            var id:Long,
                            var keyNo:String,
                            var crId:String,
                            var category:String,
                            var name:String,
                            var owner:String,
                            var registerNo:String,
                            var registerDate:String,
                            var finishDate:String,
                            var publishDate:String

                           )
